package com.ruoyi.web.controller.applets;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.LevelCommentDto;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.task.domain.TaskComment;
import com.ruoyi.task.service.ITaskCommentService;
import com.ruoyi.wechat.domain.WechatUser;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruoyi.common.constant.HttpStatus.CONFLICT;
import static com.ruoyi.common.constant.HttpStatus.LOGOUT;

/**
 * 任务评论Controller
 *
 * @author KevenPotter
 * @date 2024-04-30 10:34:28
 */
@RestController
@RequestMapping("/applets/task/comment")
@AllArgsConstructor
public class AppletsTaskCommentController extends BaseController {

    private final ITaskCommentService taskCommentService;
    private final TokenService tokenService;

    /**
     * 查询任务评论列表
     *
     * @param taskComment 任务评论对象
     * @return 返回任务列表
     * @author KevenPotter
     * @date 2024-04-30 09:41:20
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(TaskComment taskComment) {
        taskComment.setReviewStatus(99);
        startPage();
        List<TaskComment> list = taskCommentService.getList(taskComment);
        list.forEach(item -> taskCommentService.filterOutbound(item, null));
        return getDataTable(list);
    }

    /**
     * 根据[任务编号]查询任务评论列表
     *
     * @param taskId 任务编号
     * @author KevenPotter
     * @date 2024-05-01 16:50:26
     */
    @Anonymous
    @GetMapping("/listByTaskId/{taskId}")
    public AjaxResult listByTaskId(@PathVariable Long taskId) {
        List<LevelCommentDto> list = taskCommentService.getLevelComment(taskId);
        return AjaxResult.success(list);
    }

    /**
     * 新增任务评论
     *
     * @param taskComment 任务评论对象
     * @return 返回是否新增成功
     * @author KevenPotter
     * @date 2023-04-30 10:02:14
     */
    @Anonymous
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TaskComment taskComment) {
        if (tokenService.checkAppletsToken()) {
            Long toUserId = taskComment.getToUserId();
            WechatUser loginWechatUser = tokenService.getLoginWechatUser();
            if (loginWechatUser.getId().equals(toUserId)) return AjaxResult.error(CONFLICT, "不可以评论自己");
            taskComment.setFromUserId(loginWechatUser.getId());
            taskComment.setFromUserName(loginWechatUser.getNickName());
            taskComment = taskCommentService.filterInbound(taskComment, null);
            return toAjax(taskCommentService.insertTaskComment(taskComment));
        }
        return AjaxResult.error(LOGOUT, "请重新登录");
    }

}
